package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvDevicerelation;
import com.ruoyi.service.IAdvDevicerelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/system/devicerelation")
public class AdvDevicerelationController extends BaseController
{
    @Autowired
    private IAdvDevicerelationService advDevicerelationService;

    /**
     * 查询列表
     */

    @PreAuthorize("@ss.hasPermi('system:devicerelation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvDevicerelation advDevicerelation)
    {
        startPage();
        List<AdvDevicerelation> list = advDevicerelationService.selectAdvDevicerelationList(advDevicerelation);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:devicerelation:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvDevicerelation advDevicerelation)
    {
        List<AdvDevicerelation> list = advDevicerelationService.selectAdvDevicerelationList(advDevicerelation);
        ExcelUtil<AdvDevicerelation> util = new ExcelUtil<AdvDevicerelation>(AdvDevicerelation.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:devicerelation:query')")
    @GetMapping(value = "/{advDevicerelationId}")
    public AjaxResult getInfo(@PathVariable("advDevicerelationId") Long advDevicerelationId)
    {
        return success(advDevicerelationService.selectAdvDevicerelationByAdvDevicerelationId(advDevicerelationId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:devicerelation:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvDevicerelation advDevicerelation)
    {
        return toAjax(advDevicerelationService.insertAdvDevicerelation(advDevicerelation));
    }

    /**
     * 修改
     */

    @PreAuthorize("@ss.hasPermi('system:devicerelation:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvDevicerelation advDevicerelation)
    {
        return toAjax(advDevicerelationService.updateAdvDevicerelation(advDevicerelation));
    }

    /**
     * 删除
     */

    @PreAuthorize("@ss.hasPermi('system:devicerelation:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advDevicerelationIds}")
    public AjaxResult remove(@PathVariable Long[] advDevicerelationIds)
    {
        return toAjax(advDevicerelationService.deleteAdvDevicerelationByAdvDevicerelationIds(advDevicerelationIds));
    }


    @PutMapping("/syncdevices")
    public SimpleResult syncDevice(@RequestBody AdvDevicerelation advDevicerelation) {
        String result = advDevicerelationService.syncDevice(advDevicerelation);
        return SimpleResult.success("同步成功");
    }


    @PutMapping("/synclive")
    public SimpleResult syncLive(@RequestBody AdvDevicerelation advDevicerelation) {
        System.out.println("获取到的参数" + advDevicerelation);
        String result = advDevicerelationService.syncLive(advDevicerelation);
        return SimpleResult.success(result);
    }

    /**
     * 修改默认播放视频
     * @param advDevicerelation
     * @return
     */

    @PutMapping("/change/default")
    public SimpleResult changeDefault(@RequestBody AdvDevicerelation advDevicerelation) {
        boolean b = advDevicerelationService.changeDefault(advDevicerelation);
        if(!b) {
            return SimpleResult.success("设置失败");
        }
        return SimpleResult.success("设置完成");
    }


    @DeleteMapping("/removebyid/{devicesno}/{advBroadcastId}")
    public SimpleResult removeById(@PathVariable("devicesno") String devicesno,
                                   @PathVariable("advBroadcastId") String advBroadcastId) {
        String result = advDevicerelationService.removeById(devicesno, advBroadcastId);
        return SimpleResult.success(result);
    }

    // 班若之声的数据修改

    @PutMapping("/updateVoice")
    public SimpleResult updateVoice(@RequestBody AdvDevicerelation advDevicerelation) {
        System.out.println("提交的数据 ===> " + advDevicerelation);
        SimpleResult result = advDevicerelationService.updateVoice(advDevicerelation);
        return result;
    }

}




